<?php
if( !defined( 'HIGHLIGHT_LINK' ) ) {
	define( 'HIGHLIGHT_LINK', 'news' );
}

if( KF_EDITOR_MODE == 'Markdown' ) {
	include_once( Foundry::config('libPath').'/markdown.php' );
} else {
	include_once( Foundry::config('libPath').'/Textile.php' );
}

class net_mode3_news_newsAdminController extends FoundryControllerInterface
{


	/**
	 * init()
	 *
	 * init() is called in place of the __construct() method used by the
	 * FoundryControllerInterface super class
	 * This function will be called immediately after the __construct method
	 *
	 */ 
	public function init()
	{
//		$model = FoundryController::model( 'net.mode3.news:news' );	
	
	}
	
	public function admin()
	{

		return true;
	}
	
	public function post()
	{
		$user = FoundryRegistry::get( 'USER_session');
		
		$id = FoundryRequest::get( 'id', 'num' );
		$post = false;
		
		$postTags = array();
		
		if( !$id || !is_numeric( $id ) ) {
			$id = false;
		} else {
			$post = FoundryController::model( 'net.mode3.news:news', $id );
			$tags = $post->tags;
			
			foreach( $tags as $tag ) {
				$postTags[] = $tag->uid;
			}
		}
		
		if( $user->checkKey() ) {
			$headline = cleanString( FoundryRequest::post( 'newsHeadline', 'alnum' ) );
			$subhead = cleanString( FoundryRequest::post( 'newsSubhead', 'alnum' ) );
			$abstract = cleanString( FoundryRequest::post( 'newsAbstract', 'alnum' ) );
			$content = cleanString( FoundryRequest::post( 'newsContent', 'alnum' ) );
			$tags = FoundryRequest::post( 'newsTagID', 'valid_array' );
			$published = FoundryRequest::post( 'newsPublished', 'num' );
			
			$allowComments = FoundryRequest::post( 'allowComments', 'num' );
			if( $allowComments != 1 ) {
				$allowComments = 0;
			}
			
			$moderateComments = FoundryRequest::post( 'moderate', 'num' );
			if( $moderateComments != 1 ) {
				$moderateComments = 0;
			}
			

			
			if( $headline && $content ) {
				
				if( $id === false ) {
					$post = FoundryController::model( 'net.mode3.news:news' );
					$post->added = time();
				}
				
				$post->headline = $headline;
				$post->subhead = $subhead;
				$post->abstract = $abstract;
				$post->content = $content;
				$post->allow_comments = $allowComments;
				$post->moderate = $moderateComments;
				
				if( $published ) {
					$post->published = 1;
				} else {
					$post->published = 0;
				}
				
				if( $allowComments == 1 ) {
					$post->challenge = cleanString( FoundryRequest::post(
						'commentChallenge', 'alnum' ) );
					$post->response = cleanString( FoundryRequest::post(
						'commentResponse', 'alnum' ) );
					$post->response = strtolower( $post->response );
				}
				
				if( KF_EDITOR_MODE == 'Markdown' ) {
					$post->abstract_markdown = Markdown( $post->abstract );
					$post->content_markdown = Markdown( $post->content );
				} else {
					$textile = new Textile;
					$abstract = preg_replace(
						"/&quot;([a-zA-Z0-9\-_ ]*)&quot;\:/",
						'"$1":',
						$post->abstract );

					$content = preg_replace(
						"/&quot;([a-zA-Z0-9\-_ ]*)&quot;\:/",
						'"$1":',
						$post->content );
				
				
					$post->abstract_markdown = $textile->process( $abstract );
					$post->content_markdown = $textile->process( $content );
				}
				
				$post->modified = strtotime( 'today' );

				$post->status_id = 1;

				$post->user_id = AUTH_USERID;
				
				$post->save();
			
				if( is_array( $tags ) && !empty( $tags ) ) {
					$post->setManyReference( 'tags', $tags );
				}
			
				header( "Location: ".
					FoundryView::helperLinkTo( 'newsAdmin/post/'.$post->uid.'/sv' ) );
				exit;
					
				
			}
		}
		
		$key = $user->generateKey();
	
		return array( 'key' => $key, 'post'	=> $post, 'postTags' => $postTags );
	}
	
	public function publish()
	{
		$id = FoundryRequest::post( 'id', 'num' );
		
		if( $id ) {
			$post = FoundryController::model( 'net.mode3.news:news', $id );
			$post->published = 1;
			
			$post->save();
			
			return array( 'saved' => 1 );
		}
	}
	
	public function deletePost()
	{
		$id = FoundryRequest::post( 'id', 'num' );
		
		if( $id ) {
			$post = FoundryController::model( 'net.mode3.news:news', $id );
			
			$post->delete();
			
			return array( 'saved' => 1 );
		}
	}

}

?>